package org.zju.demo.shoppingdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import net.tsz.afinal.FinalBitmap;

import org.zju.demo.shoppingdemo.R;
import org.zju.demo.shoppingdemo.application.ShoppingApplication;
import org.zju.demo.shoppingdemo.controller.ShoppingController;
import org.zju.demo.shoppingdemo.model.Product;
import org.zju.demo.shoppingdemo.model.Request.GetProductModel;
import org.zju.demo.shoppingdemo.model.response.BaseResponse;
import org.zju.demo.shoppingdemo.model.response.ResponseProductDetailJson;
import org.zju.demo.shoppingdemo.utils.Constant;

import java.util.List;

/**
 * Created by ZJDX on 2015/9/13.
 */
public class ProductDetailActivity extends CommonActivity {
    private ImageView itemImageView;
    private TextView itemNameView;
    private TextView itemPriceView;
    private TextView itemAmountView;
    private Button buyButton;
    private Button cartButton;
    private Button transBtn;
    private Intent startIntent;
    int width;
    int height;
    Product currentProduct;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 不显示标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.product_details);
        startIntent = this.getIntent();
        initViewComponent();
        initBottomMenu();
        requestData();

        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        width = metric.widthPixels;
        height = metric.heightPixels;
    }

    private void initViewComponent() {
        itemAmountView = (TextView) findViewById(R.id.amount);
        itemImageView = (ImageView) findViewById(R.id.gooditem_photo);
        itemNameView = (TextView) findViewById(R.id.brief);
        itemPriceView = (TextView) findViewById(R.id.price);
        buyButton = (Button) findViewById(R.id.buy_now);
        cartButton = (Button) findViewById(R.id.add_to_cart);
        Listener listener = new Listener();
        buyButton.setOnClickListener(listener);
        cartButton.setOnClickListener(listener);
    }

    private class Listener implements View.OnClickListener {
        List<Product> products;
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.buy_now:
                    //jump to cart activity
                    products = ((ShoppingApplication)getApplication()).getShoppingCart();
                    if(!products.contains(currentProduct)) {
                        currentProduct.setNumber(1);
                        products.add(currentProduct);
                    }
                    Intent intent = new Intent();
                    intent.setClass(ProductDetailActivity.this, CartActivity.class);
                    startActivity(intent);
                    break;
                case R.id.add_to_cart:
                    // add the item to cart
                    products = ((ShoppingApplication)getApplication()).getShoppingCart();
                    if(!products.contains(currentProduct)) {
                        currentProduct.setNumber(1);
                        products.add(currentProduct);
                    } else {
                        Toast.makeText(ProductDetailActivity.this, "已经加入到购物车中", Toast.LENGTH_SHORT).show();
                    }
                    break;
                default:
            }
        }
    }

    private void requestData(){
        int productId = startIntent.getIntExtra("productId", -1);
        if (productId == -1) {
            finish();
        }
        GetProductModel model = new GetProductModel();
        model.setCommandId(Constant.COMMAND_QUERYPRODUCT);
//        model.setLatitude();
        model.setProductId(productId);
        model.setUsername(((ShoppingApplication) getApplication()).getUsername());
        ShoppingController.doQueryProduct(this, model);
    }
    @Override
    public void onResponseMessage(BaseResponse response) {
        super.onResponseMessage(response);
        currentProduct = new Product();
        ResponseProductDetailJson product = (ResponseProductDetailJson)response;
        currentProduct.setProductId(startIntent.getIntExtra("productId", -1));
        currentProduct.setPrice(product.getProductPrice());
        currentProduct.setProductName(product.getProductName());
        currentProduct.setAmount(product.getProductAmount());
        currentProduct.setImageUrl(product.getProductImage());
        FinalBitmap fb = ((ShoppingApplication)getApplication()).getFinalBitmap();
        Log.e("ProductDetail", product.getProductImage());
        String url = "http://10.214.143.25:8080/" + product.getProductImage();
        int dWidth = (int)(width * 0.8);
        int dHeight = (int)(height * 0.6);
        fb.display(itemImageView, url, dWidth, dHeight);
        itemNameView.setText(product.getProductName());
        itemPriceView.setText(Float.toString(product.getProductPrice()));
        itemAmountView.setText(Float.toString(product.getProductAmount()));
    }

    @Override
    public void initBottomMenu() {
        Button transBtn;
        transBtn=(Button)findViewById(R.id.transButton);
        transBtn.getBackground().setAlpha(200);//更改半透明度
        imageViewIndex = (ImageView) findViewById(R.id.menu_home_img);
//        imageViewIndex.setOnTouchListener(indexListener);
        imageViewIndex.setImageResource(R.drawable.menu_home_released);
        imageViewShopping = (ImageView) findViewById(R.id.menu_shopping_cart_img);
        imageViewShopping.setOnTouchListener(shoppingListener);
        imageViewShopping.setImageResource(R.drawable.menu_shopping_cart_released);
        imageViewMyInfo = (ImageView) findViewById(R.id.menu_personal_center_image);
        imageViewMyInfo.setOnTouchListener(myInfoListener);
        imageViewMyInfo.setImageResource(R.drawable.menu_my_info_released);
    }
}
